
#import
from numpy import *

#define module globals for tables
colors = {};
tetrominos = {};
colormappings= {};

#define module globals for state
colormap_selection= None

#define classes

# A class representing tetromino color.
# Its constructor will conditionally embed the color in a dictionary if argument is supplied
class Color:
    def __init__(self, name= "unknown", rByte=0, gByte=0, cByte=0, table= None):
        self.name= name
        self.rByte= rByte
        self.gByte= gByte
        self.cByte= cByte
        if(table != None):
            table[self.name]= self;



# A class representing a tetromino.
# Its constructor will conditionally embed the tetromino in a dictionary if argument is supplied
class Tetromino:
    def __init__(self, name= "unknown", shape= None, table= None):
        self.name= name
        self.shape= shape
        if(table != None):
            table[self.name]= self


class ShapeColorMap:
    def __init__(self, shortname= None, longname= None, table=None):
        self.shortname= shortname
        self.longname= longname
        self.shapecolors= {}
        if(table != None):
            table[self.shortname]= self

    def append_colorname_for_shapename(self, shapename, colorname):
        self.shapecolors[shapename]= colorname


#create a table of colors
Color("maroon", 128,0,0, colors)
Color("white", 255,255,255, colors)
Color("magenta", 255,0,255, colors)
Color("dark blue", 0,0,139, colors)
Color("green", 0,128,0, colors)
Color("brown", 165,42,42, colors)
Color("cyan", 0,255,255, colors)
Color("red", 255,0,0, colors)
Color("yellow", 255,255,0, colors)
Color("blue", 0,0,255, colors)
Color("light gray", 211,211,211, colors)
Color("green", 0,128,0, colors)
Color("orange", 255,165,0, colors)
Color("blue-violet", 138,43,226, colors)
Color("purple", 128,0,128, colors)
Color("olive", 128,128,0, colors)

#Create the table of tetrominos
Tetromino("I", matrix('1 1 1 1'), tetrominos)
Tetromino("J", matrix('1 1 1; 0 0 1'), tetrominos)
Tetromino("L", matrix('1 1 1; 1 0 0'), tetrominos)
Tetromino("O", matrix('1 1; 1 1'), tetrominos)
Tetromino("S", matrix('0 1 1; 1 1 0'), tetrominos)
Tetromino("T", matrix('1 1 1; 0 1 0'), tetrominos)
Tetromino("Z", matrix('1 1 0; 0 1 1'), tetrominos)

#create the color maps
# Vadim Gerasimov's Tetris 3.12
tempmap= ShapeColorMap("Gerasimov", "Vadim Gerasimov's Tetris 3.12", colormappings)
tempmap.append_colorname_for_shapename("I", "maroon")
tempmap.append_colorname_for_shapename("J", "white")
tempmap.append_colorname_for_shapename("L", "magenta")
tempmap.append_colorname_for_shapename("O", "dark blue")
tempmap.append_colorname_for_shapename("S", "green")
tempmap.append_colorname_for_shapename("T", "brown")
tempmap.append_colorname_for_shapename("Z", "cyan")
# Microsoft Tetris
tempmap= ShapeColorMap("Microsoft", "Microsoft Tetris", colormappings)
tempmap.append_colorname_for_shapename("I", "red")
tempmap.append_colorname_for_shapename("J", "magenta")
tempmap.append_colorname_for_shapename("L", "yellow")
tempmap.append_colorname_for_shapename("O", "cyan")
tempmap.append_colorname_for_shapename("S", "blue")
tempmap.append_colorname_for_shapename("T", "light gray")
tempmap.append_colorname_for_shapename("Z", "green")
# Sega/Arika (TGM series)
tempmap= ShapeColorMap("Sega", "Sega/Arika (TGM series)", colormappings)
tempmap.append_colorname_for_shapename("I", "red")
tempmap.append_colorname_for_shapename("J", "blue")
tempmap.append_colorname_for_shapename("L", "orange")
tempmap.append_colorname_for_shapename("O", "yellow")
tempmap.append_colorname_for_shapename("S", "magenta")
tempmap.append_colorname_for_shapename("T", "cyan")
tempmap.append_colorname_for_shapename("Z", "green")
# The New Tetris
tempmap= ShapeColorMap("New", "The New Tetris", colormappings)
tempmap.append_colorname_for_shapename("I", "cyan")
tempmap.append_colorname_for_shapename("J", "blue-violet")
tempmap.append_colorname_for_shapename("L", "magenta")
tempmap.append_colorname_for_shapename("O", "light gray")
tempmap.append_colorname_for_shapename("S", "green")
tempmap.append_colorname_for_shapename("T", "yellow")
tempmap.append_colorname_for_shapename("Z", "red")
# The Tetris Company standardization (Tetris Worlds, Tetris Party...)
empmap= ShapeColorMap("Tetris Company", "The Tetris Company standardization (Tetris Worlds, Tetris Party...)", colormappings)
tempmap.append_colorname_for_shapename("I", "cyan")
tempmap.append_colorname_for_shapename("J", "blue")
tempmap.append_colorname_for_shapename("L", "orange")
tempmap.append_colorname_for_shapename("O", "yellow")
tempmap.append_colorname_for_shapename("S", "green")
tempmap.append_colorname_for_shapename("T", "purple")
tempmap.append_colorname_for_shapename("Z", "red")
# Atari/Arcade
empmap= ShapeColorMap("Atari", "Atari/Arcade", colormappings)
tempmap.append_colorname_for_shapename("I", "red")
tempmap.append_colorname_for_shapename("J", "yellow")
tempmap.append_colorname_for_shapename("L", "magenta")
tempmap.append_colorname_for_shapename("O", "blue")
tempmap.append_colorname_for_shapename("S", "green")
tempmap.append_colorname_for_shapename("T", "olive")
tempmap.append_colorname_for_shapename("Z", "cyan")
# TETRIS The Soviet Mind Game
tempmap= ShapeColorMap("Soviet", "TETRIS The Soviet Mind Game", colormappings)
tempmap.append_colorname_for_shapename("I", "red")
tempmap.append_colorname_for_shapename("J", "orange")
tempmap.append_colorname_for_shapename("L", "magenta")
tempmap.append_colorname_for_shapename("O", "blue")
tempmap.append_colorname_for_shapename("S", "green")
tempmap.append_colorname_for_shapename("T", "olive")
tempmap.append_colorname_for_shapename("Z", "cyan")


#define utility functions
#TODO Use fliplr and flipud here but fix them to operate on 1D matrices with conditional nullop.
def rotate_right(amatrix):
    transpose_m= amatrix.transpose()
    rotated_m= transpose_m


